package com.fjnldx.mes.controller.alarm;

import com.fjnldx.mes.domain.domainReq.DeleteReq;
import com.fjnldx.mes.domain.domainReq.Id;
import com.fjnldx.mes.domain.domainReq.IdList;
import com.fjnldx.mes.domain.domainReq.alarm.AddAlarmConfigReq;
import com.fjnldx.mes.domain.domainReq.alarm.EditAlarmConfigReq;
import com.fjnldx.mes.domain.domainReq.alarm.QueryAlarmConfigPageReq;
import com.fjnldx.mes.domain.domainReq.alarm.QueryAlarmRecordPageReq;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.service.alarm.AlarmConfigService;
import com.fjnldx.mes.service.alarm.AlarmRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/alarm")
@Api(value = "告警配置", tags = "告警管理")
public class AlarmConfigController {
    @Resource
    AlarmConfigService service;

    @Resource
    AlarmRecordService recordService;

    @GetMapping("/config/page")
    @ApiOperation(value = "告警列表", notes = "告警配置项列表")
    public Result queryConfigPage(QueryAlarmConfigPageReq req) throws Exception {
        return service.getPages(req);
    }

    @GetMapping("/config/detail")
    @ApiOperation(value = "告警配置详情", notes = "告警配置详情")
    public Result getConfigDetail(Id id) throws Exception {
        return service.getDetail(id);
    }

    @PostMapping("/config/add")
    @ApiOperation(value = "添加告警配置", notes = "添加告警配置")
    public Result addConfig(@RequestBody @Valid AddAlarmConfigReq param) throws Exception {
        return service.addConfig(param);
    }

    @PostMapping("/config/delete")
    @ApiOperation(value = "删除告警配置", notes = "删除告警配置")
    public Result addConfig(@RequestBody DeleteReq param) throws Exception {
        return service.deleteConfig(param);
    }

    @PostMapping("/config/update")
    @ApiOperation(value = "更新告警配置", notes = "更新告警配置")
    public Result editConfig(@RequestBody @Valid EditAlarmConfigReq param) throws Exception {
        return service.editConfig(param);
    }

    @PostMapping("/config/active")
    @ApiOperation(value = "上线告警配置", notes = "上线告警配置")
    public Result activeConfig(@RequestBody @Valid IdList param) throws Exception {
        return service.activeConfig(param, true);
    }

    @PostMapping("/config/offline")
    @ApiOperation(value = "下线告警配置", notes = "下线告警配置")
    public Result offlineConfig(@RequestBody @Valid IdList param) throws Exception {
        return service.activeConfig(param, false);
    }

    @GetMapping("/config/record/count")
    @ApiOperation(value = "获取告警数", notes = "获取告警数")
    public Result getCount(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) throws Exception {
        return recordService.getRecordCount(startTime, endTime);
    }

    @GetMapping("/config/record/page")
    @ApiOperation(value = "告警详情列表", notes = "告警详情列表")
    public Result getRecordPage(QueryAlarmRecordPageReq param) throws Exception {
        return recordService.getRecordPages(param);
    }

    @PostMapping("/config/record/off")
    @ApiOperation(value = "关闭告警", notes = "关闭告警")
    public Result turnOffAlarm(@RequestBody Id id) throws Exception {
        return recordService.turnOffAlarm(id);
    }

    @GetMapping("/config/record/statistical/gongxu")
    @ApiOperation(value = "获取工序统计", notes = "获取工序统计")
    public Result getGongXuStatistical(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) throws Exception {
        List<String> gongXuList = new ArrayList<>();
        gongXuList.add("细纱");
        gongXuList.add("粗纱");
        gongXuList.add("络筒");
        gongXuList.add("抓棉");
        gongXuList.add("并条");
        gongXuList.add("梳棉");
        gongXuList.add("开棉");
        gongXuList.add("多仓");
        return recordService.getStatistical(gongXuList, startTime, endTime);
    }

    @GetMapping("/config/record/statistical/fuji")
    @ApiOperation(value = "获取辅机统计", notes = "获取辅机统计")
    public Result getFuJiStatistical(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) throws Exception {
        List<String> fujiList = new ArrayList<>();
        fujiList.add("电表");
        fujiList.add("温湿度");
        fujiList.add("空压机");
        return recordService.getStatistical(fujiList, startTime, endTime);
    }

    @GetMapping("/config/record/statistical/level")
    @ApiOperation(value = "获取告警等级统计", notes = "获取告警等级统计")
    public Result getLevelStatistical(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) throws Exception {
        return recordService.getLevelStatistical(startTime, endTime);
    }
}
